/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.warehouse.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.warehouse.entity.Material;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.thinkgem.jeesite.modules.warehouse.dao.MaterialDao;

/**
 * MaterialService
 * 
 * @author qrg
 * @version 2017-03-27
 */
@Service
@Transactional(readOnly = true)
public class MaterialService extends CrudService<MaterialDao, Material> {

	@Autowired
	private MaterialDao materialDao;

	public Material get(String id) {
		return super.get(id);
	}

	public List<Material> findList(Material material) {
		return super.findList(material);
	}

	public List<Material> findAllByOffice(Office office) {
		return materialDao.findAllByOffice(office);
	}

	public Page<Material> findPage(Page<Material> page, Material material) {
		material.getSqlMap().put("dsf", dataScopeFilter(material.getCurrentUser(),"o10","u" ));
		material.setPage(page);
		page.setList(materialDao.findList(material));
		return page;
	}

	@Transactional(readOnly = false)
	public void save(Material material) {

		// 记录当前用户所属公司
		Office office = UserUtils.getUser().getOffice();
		material.setOffice(office);

		// 保存物料
		super.save(material);
	}

	@Transactional(readOnly = false)
	public void updateStock(Material material, String inout, double changeStock) {
		Double stock = material.getStock();
		if (stock == null){
			stock = 0.0;
		}
		if (inout.equals("INBOUND")) {
			stock += changeStock;
		} else {
			stock -= changeStock;
		}
		material.setStock(stock);
		save(material);
	}

	@Transactional(readOnly = false)
	public void delete(Material material) {
		super.delete(material);
	}

}